package com.qinsong.myapplication.model.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.INotificationSideChannel;

import com.qinsong.myapplication.model.bean.GroupInfo;
import com.qinsong.myapplication.model.bean.InvitationInfo;
import com.qinsong.myapplication.model.bean.UserInfo;
import com.qinsong.myapplication.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 邀请信息dao
 */
public class InvitationDao {
    private DBHelper mHelper;
    // 将int类型状态转换为邀请的状态
    private InvitationInfo.InvitationStatus int2InviteStatus(int intStatus) {

        if (intStatus == InvitationInfo.InvitationStatus.NEW_INVITE.ordinal()) {
            return InvitationInfo.InvitationStatus.NEW_INVITE;
        }

        if (intStatus == InvitationInfo.InvitationStatus.INVITE_ACCEPT.ordinal()) {
            return InvitationInfo.InvitationStatus.INVITE_ACCEPT;
        }

        if (intStatus == InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER.ordinal()) {
            return InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER;
        }

        if (intStatus == InvitationInfo.InvitationStatus.NEW_GROUP_INVITE.ordinal()) {
            return InvitationInfo.InvitationStatus.NEW_GROUP_INVITE;
        }

        if (intStatus == InvitationInfo.InvitationStatus.NEW_GROUP_APPLICATION.ordinal()) {
            return InvitationInfo.InvitationStatus.NEW_GROUP_APPLICATION;
        }

        if (intStatus == InvitationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED.ordinal()) {
            return InvitationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED;
        }

        if (intStatus == InvitationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED.ordinal()) {
            return InvitationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED;
        }

        if (intStatus == InvitationInfo.InvitationStatus.GROUP_INVITE_DECLINED.ordinal()) {
            return InvitationInfo.InvitationStatus.GROUP_INVITE_DECLINED;
        }

        if (intStatus == InvitationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED.ordinal()) {
            return InvitationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED;
        }

        if (intStatus == InvitationInfo.InvitationStatus.GROUP_ACCEPT_INVITE.ordinal()) {
            return InvitationInfo.InvitationStatus.GROUP_ACCEPT_INVITE;
        }

        if (intStatus == InvitationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION.ordinal()) {
            return InvitationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION;
        }

        if (intStatus == InvitationInfo.InvitationStatus.GROUP_REJECT_APPLICATION.ordinal()) {
            return InvitationInfo.InvitationStatus.GROUP_REJECT_APPLICATION;
        }

        if (intStatus == InvitationInfo.InvitationStatus.GROUP_REJECT_INVITE.ordinal()) {
            return InvitationInfo.InvitationStatus.GROUP_REJECT_INVITE;
        }

        return null;
    }
    public InvitationDao(DBHelper helper) {
        mHelper = helper;
    }

    /**
     *  添加邀请
     * @param invitationInfo
     */
    public void addInvitation(InvitationInfo invitationInfo) {
        //获取数据库连接
        SQLiteDatabase db = mHelper.getReadableDatabase();



        ContentValues values = new ContentValues();
        values.put(InvitationTable.COL_REASON,invitationInfo.getReason());//邀请原因
        values.put(InvitationTable.COL_STATUS,invitationInfo.getStatus().ordinal());//邀请状态
        //判断邀请信息是联系人的还是群组信息
        UserInfo userInfo = invitationInfo.getUserInfo();

            //联系人
            values.put(InvitationTable.COL_USER_HXID,invitationInfo.getUserInfo().getHxid());
            values.put(InvitationTable.COL_USER_NAME,invitationInfo.getUserInfo().getName());

        //执行添加语句
        db.replace(InvitationTable.TAB_NAME,null,values);

    }

    /**
     * 获取所有邀请信息
     * @return
     */
    public List<InvitationInfo> getInvitations() {
        //获取数据库连接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行查询语句
        String sql = "select * from " + InvitationTable.TAB_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        List<InvitationInfo> invitationInfos = new ArrayList<>();
        while (cursor.moveToNext()){
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setReason(cursor.getString(cursor.getColumnIndex(InvitationTable.COL_REASON)));
            //邀请状态
            invitationInfo.setStatus(int2InviteStatus(cursor.getInt(cursor.getColumnIndex(InvitationTable.COL_STATUS))));
            String groupid = cursor.getString(cursor.getColumnIndex(InvitationTable.COL_GROUP_HXID));

                //联系人信息
                UserInfo userInfo = new UserInfo();
                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(InvitationTable.COL_USER_HXID)));
                userInfo.setName(cursor.getString(cursor.getColumnIndex(InvitationTable.COL_USER_NAME)));
                userInfo.setNichen(cursor.getString(cursor.getColumnIndex(InvitationTable.COL_USER_NAME)));
                invitationInfo.setUserInfo(userInfo);

            invitationInfos.add(invitationInfo);
        }
        //关闭资源连接
        cursor.close();
        //返回数据
        return  invitationInfos;

    }

    /**
     * 删除邀请信息
     * @param hxId
     */
    public void removeInvitation(String hxId) {
        if (hxId == null) {
            return;
        }

        // 获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        // 执行删除语句
        db.delete(InvitationTable.TAB_NAME, InvitationTable.COL_USER_HXID + "=?", new String[]{hxId});
    }

    /**
     * 更新邀请状态
     * @param invitationStatus
     * @param hxId
     */
    public void updateInvitationStatus(InvitationInfo.InvitationStatus invitationStatus, String hxId) {
        if (hxId == null) {
            return;
        }

        // 获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        // 执行更新操作
        ContentValues values = new ContentValues();
        values.put(InvitationTable.COL_STATUS, invitationStatus.ordinal());

        db.update(InvitationTable.TAB_NAME, values, InvitationTable.COL_USER_HXID + "=?", new String[]{hxId});
    }
    }


